package com.seeyon.apps.schedule.manager.impl;

import com.seeyon.apps.schedule.manager.WorkPlanManager;
import com.seeyon.apps.schedule.util.WorkPlanUtil;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.cap4.form.modules.engin.base.formData.CAP4FormDataDAO;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumBean;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.util.DateUtil;
import com.seeyon.ctp.util.FlipInfo;
import com.seeyon.ctp.util.JDBCAgent;
import com.seeyon.ctp.util.Strings;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WorkPlanManagerImpl implements WorkPlanManager {
    private FormApi4Cap4 formApi4Cap4;
    private CAP4FormDataDAO cap4FormDataDAO;

    public void setFormApi4Cap4(FormApi4Cap4 formApi4Cap4) {
        this.formApi4Cap4 = formApi4Cap4;
    }

    public void setCap4FormDataDAO(CAP4FormDataDAO cap4FormDataDAO) {
        this.cap4FormDataDAO = cap4FormDataDAO;
    }

    @Override
    public FlipInfo getDetails(FlipInfo flipInfo, Map<String, Object> params) {
        //请假信息底表-市委底表表单编号
        String workPlanForm = AppContext.getSystemProperty("workplan.workPlanForm");
        if (Strings.isEmpty(workPlanForm)) {
            return null;
        }

        try {
            FormBean formBeanSW = formApi4Cap4.getFormByFormCode(workPlanForm);
            FormFieldBean writeTimeField = formBeanSW.getFieldBeanByDisplay("填写日期");
            String writeTimeSW = writeTimeField.getName();//填写日期
            String mainTableName = formBeanSW.getMasterTableBean().getTableName();

            FormFieldBean workInfoField = formBeanSW.getFieldBeanByDisplay("工作内容");
            String workInfoSW = workInfoField.getName();//工作内容
            String sonTableName = workInfoField.getOwnerTableName();
            String timeAddressSW = formBeanSW.getFieldBeanByDisplay("地点").getName();//时间地点
            String descSW = formBeanSW.getFieldBeanByDisplay("备注").getName();//备注

            StringBuffer sql = new StringBuffer();
            sql.append("  SELECT son.").append(timeAddressSW).append(" as timeAddress, son.").append(workInfoSW).append(" as workInfo, son.").append(descSW).append(" as ddesc ");
            sql.append(" FROM ").append(sonTableName).append(" son ");
            sql.append(" JOIN ").append(mainTableName).append(" main ON son.formmain_id = main.id");
            sql.append(" where 1 = 1  ");
            sql.append(" and main.").append(writeTimeSW).append(" =? ");
            sql.append(" order by son.sort ");
            List<Object> paramSQL = new ArrayList<>();
            if (params.containsKey("date")) {
                paramSQL.add(DateUtil.toDate((String)params.get("date")));
            } else {
                paramSQL.add(new Date(System.currentTimeMillis()));
            }

            FlipInfo info = cap4FormDataDAO.selectDataBySql(sql.toString(), paramSQL, flipInfo);
            List<Map<String, Object>> data = info.getData();
            List<Map<String, Object>> data1 = new ArrayList<>();
            for (Map<String, Object> row : data) {
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("timeAddress", row.get("timeaddress") == null ? "" : row.get("timeaddress"));
                hashMap.put("workInfo", row.get("workinfo") == null ? "" : row.get("workinfo"));
                hashMap.put("desc", row.get("ddesc") == null ? "" : row.get("ddesc"));
                data1.add(hashMap);
            }
            info.setData(data1);
            return info;
        } catch (Exception e) {
            WorkPlanUtil.chenfan("获取工作安排列表", "WorkPlanManagerImpl.getDetails", e);
        }
        return null;
    }

    public FlipInfo getDatas(FlipInfo flipInfo, String sql) {
        JDBCAgent jdbcAgent = new JDBCAgent(true);
        try {
            flipInfo = jdbcAgent.findNameByPaging(sql, new HashMap<>(), flipInfo);
            return flipInfo;
        } catch (Exception e) {
            WorkPlanUtil.chenfan("获取工作安排列表失败", "WorkPlanManagerImpl.getDetails", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return null;
    }
}
